﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;

namespace store.cyt369.com.models.Identity
{
    public class UserStore : UserStoreBase<SysUser, int, UserClaim, UserLogin, UserToken>
    {
        private string m_UserServiceURL;
        public UserStore(IdentityErrorDescriber describer) : base(describer)
        {
            m_UserServiceURL = store.cyt369.com.utility.SystemSettingService.Instance.UserServiceURL;
        }

        public override IQueryable<SysUser> Users
        {
            get
            {
                return null;
            }
        }

        public override Task AddClaimsAsync(SysUser user, IEnumerable<Claim> claims, CancellationToken cancellationToken = default)
        {
            return Task.CompletedTask;
        }

        public override Task AddLoginAsync(SysUser user, UserLoginInfo login, CancellationToken cancellationToken = default)
        {
            return Task.CompletedTask;
        }

        public override Task<IdentityResult> CreateAsync(SysUser user, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        public override Task<IdentityResult> DeleteAsync(SysUser user, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(IdentityResult.Success);
        }

        public override Task<SysUser> FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        public override Task<SysUser> FindByIdAsync(string userId, CancellationToken cancellationToken = default)
        {
            var headers = new store.cyt369.com.models.WebRequest.HttpHeaderList();
            headers.Add("SSO", store.cyt369.com.utility.SystemSettingService.Instance.AppID);

            var args = new store.cyt369.com.models.WebRequest.HttpParameterList();
            args.Add("appId", store.cyt369.com.utility.SystemSettingService.Instance.AppID);
            args.Add("appKey", store.cyt369.com.utility.SystemSettingService.Instance.AppKey);
            args.Add("userId", userId);

            var url = store.cyt369.com.models.WebRequest.URLHelper.CombinURL(m_UserServiceURL, "FindUserById");
            var response = store.cyt369.com.models.WebRequest.Browser.PostUrl(url, args, headers);
            if (response != null)
            {
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var wcfUser = System.Text.Json.JsonSerializer.Deserialize<WcfUser>(response.ContentString);
                    if (wcfUser != null)
                    {
                        if (wcfUser.Result)
                        {
                            return Task.FromResult(wcfUser.Data);
                        }
                        else
                        {
                            throw new Exception(wcfUser.Msg);
                        }
                    }
                }
                else
                {
                    throw new Exception("HTTP ERROR");
                }
            }

            return null;
        }

        public override Task<SysUser> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken = default)
        {
            var headers = new store.cyt369.com.models.WebRequest.HttpHeaderList();
            headers.Add("SSO", store.cyt369.com.utility.SystemSettingService.Instance.AppID);

            var args = new store.cyt369.com.models.WebRequest.HttpParameterList();
            args.Add("appId", store.cyt369.com.utility.SystemSettingService.Instance.AppID);
            args.Add("appKey", store.cyt369.com.utility.SystemSettingService.Instance.AppKey);
            args.Add("ticket", normalizedUserName);

            var url = store.cyt369.com.models.WebRequest.URLHelper.CombinURL(m_UserServiceURL, "CheckTicket");
            var response = store.cyt369.com.models.WebRequest.Browser.PostUrl(url, args, headers);
            if (response != null)
            {
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var wcfUser = System.Text.Json.JsonSerializer.Deserialize<WcfUser>(response.ContentString);
                    if (wcfUser != null)
                    {
                        if (wcfUser.Result)
                        {
                            return Task.FromResult(wcfUser.Data);
                        }
                        else
                        {
                            throw new Exception(wcfUser.Msg);
                        }
                    }
                }
                else
                {
                    throw new Exception("HTTP ERROR");
                }
            }

            return null;
        }

        public override Task<IList<Claim>> GetClaimsAsync(SysUser user, CancellationToken cancellationToken = default)
        {
            var list = new List<Claim>() as IList<Claim>;
            return Task.FromResult(list);
        }

        public override Task<IList<UserLoginInfo>> GetLoginsAsync(SysUser user, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        public override Task<IList<SysUser>> GetUsersForClaimAsync(Claim claim, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        public override Task RemoveClaimsAsync(SysUser user, IEnumerable<Claim> claims, CancellationToken cancellationToken = default)
        {
            return Task.CompletedTask;
        }

        public override Task RemoveLoginAsync(SysUser user, string loginProvider, string providerKey, CancellationToken cancellationToken = default)
        {
            return Task.CompletedTask;
        }

        public override Task ReplaceClaimAsync(SysUser user, Claim claim, Claim newClaim, CancellationToken cancellationToken = default)
        {
            return Task.CompletedTask;
        }

        public override Task<IdentityResult> UpdateAsync(SysUser user, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        protected override Task AddUserTokenAsync(UserToken token)
        {
            return Task.CompletedTask;
        }

        protected override Task<UserToken> FindTokenAsync(SysUser user, string loginProvider, string name, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        protected override Task<SysUser> FindUserAsync(int userId, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        protected override Task<UserLogin> FindUserLoginAsync(int userId, string loginProvider, string providerKey, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        protected override Task<UserLogin> FindUserLoginAsync(string loginProvider, string providerKey, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        protected override Task RemoveUserTokenAsync(UserToken token)
        {
            return Task.CompletedTask;
        }
    }
}
